package cn.hpu.mobile_bank.controller;

import cn.hpu.mobile_bank.dto.req.LoginFormReq;
import cn.hpu.mobile_bank.dto.req.RegisterReq;
import cn.hpu.mobile_bank.dto.resp.ApiResult;
import cn.hpu.mobile_bank.dto.resp.LoginResponse;
import cn.hpu.mobile_bank.service.MobileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Tag(name = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private MobileService mobileService;

    @PostMapping("/login")
    @Operation(summary = "登录")
    public ApiResult<LoginResponse> login(@RequestBody @Valid LoginFormReq loginFormReq) {
        return mobileService.login(loginFormReq);
    }

    /**
     * 发送手机验证码
     */
    @GetMapping("/code")
    @Operation(summary = "发送手机验证码")
    public ApiResult<String> sendCode(@RequestParam("phone") String phone) {
        //发送短信验证码并保存验证码
        return mobileService.sendCode(phone);
    }

    /**
     * 校验验证码
     */
    @PostMapping("/check")
    @Operation(summary = "校验验证码")
    public ApiResult<String> checkCode(@RequestBody @Valid RegisterReq registerReq){
        return mobileService.checkCode(registerReq);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public ApiResult<String> register(@RequestBody @Valid RegisterReq register){
        return mobileService.register(register);
    }

}
